import 'package:flutter/material.dart';
import 'package:test_flutter/dio/DioUtil.dart';
import 'package:test_flutter/model/response/service_model.dart';
import 'package:test_flutter/pages/LaundryDetailPage.dart';
import 'package:test_flutter/utils/factory/Fit.dart';
import 'package:test_flutter/utils/overLay_util.dart';
import 'package:test_flutter/widgets/LaundryHero.dart';
import 'package:test_flutter/utils/ToastUtil.dart';
import 'package:test_flutter/dio/Api.dart';

bool hasInit =false;

class LaundryPage extends StatefulWidget {
  LaundryPage({Key key}) : super(key: key);
  @override
  _LaundryPage createState() => new _LaundryPage();
}

/*var list = [
  {
    'name': '柔和洗涤',
    'washTime': '约36分钟',
    'description': '数量较少，不太脏的衣物、轻薄不耐洗的衣物',
    'price':'\$2.88',
    'img': 'images/wash.png',
    'color': Colors.green,
    'colorList': [Color(0xFF18D48B), Color(0xFFFFF68F)]
  },
  {
    'name': '标准洗涤',
    'washTime': '约40分钟',
    'description': '标准的洗涤程序，平衡洗涤时间和洗净度，适用于大部分日常衣物',
    'price':'\$5.48',

    'img': 'images/wash.png',
    'color': Colors.lightGreen,
    'colorList': [Color(0xFFFF8318), Color(0xFFFFC0CB)]
  },
  {
    'name': '加强洗涤',
    'washTime': '约50分钟',
    'description': '大件厚重，特别脏，耐洗的衣物，厚牛仔装，床上用品等',
    'price':'\$8.88',

    'img': 'images/wash.png',
    'color': Colors.greenAccent,
    'colorList': [Color(0xFF1592E5), Color(0xFFAB82FF)]
  },
  {
    'name': '超强洗',
    'washTime': '约50分钟',
    'description': '大件厚重，特别脏，耐洗的衣物，厚牛仔装，床上用品等',
    'price':'\$10.98',

    'img': 'images/wash.png',
    'color': Colors.greenAccent,
    'colorList': [Color(0xFFAB82FF), Color(0xFFF86660)]

  }
];*/
var list = new List();

class _LaundryPage extends State<LaundryPage> {
  //页面初始化状态的方法
// 这个key用来在不是手动下拉，而是点击某个button或其它操作时，代码直接触发下拉刷新
  final GlobalKey<RefreshIndicatorState> _refreshIndicatorKey = new GlobalKey<RefreshIndicatorState>();

  @override
  void initState() {
    super.initState();
    if(!hasInit){
      _getDefaultPolicy();
    }

    hasInit = true;

  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      body: RefreshIndicator(
        key: _refreshIndicatorKey,
        onRefresh: _pullToRefresh, // onRefresh 参数是一个 Future<Null> 的回调
        child: ListView.builder(
          padding: EdgeInsets.only(
              left: Fit.scaleX(30),
              top: Fit.scaleY(40),
              right: Fit.scaleX(30),
              bottom: Fit.scaleY(40)),
          itemCount: list.length,
          itemBuilder: (BuildContext context, int position) {
            return  getItem(position);
          },
        ),
      ),
    );
  }

  getItem(int position) {
    Map<String, dynamic> bean = list[position];
    var row = GestureDetector(
      onTap: () {
        Navigator.push<Object>(context,
            new MaterialPageRoute(builder: (BuildContext context) {
          return new LaundryDetail(position,ServiceModel.fromJson(bean));
        }));
      },
      child: new LaundryHero(position,ServiceModel.fromJson(bean)),
    );
    return Card(
      elevation: Fit.scaleX(20),
      child: row,
    );
  }


  Future<Null> _pullToRefresh() async {
    _getDefaultPolicy();
    return null;
  }

  ///获取洗衣策略列表
  _getDefaultPolicy() async {

    DioUtil.getInstance(context).get(Api.getDefaultPolicy+'1', (response) {
      if (response.data['code'] == 1) {
        list = response.data['data']['service'];
        setState(() {
          //list = response.data['data']['list'];
        });
      } else {
        ToastUtil.showToast(response.data['msg']);
      }

    }, errorCallBack: (errorMsg) {
      print("error=========================:" + errorMsg);
      ToastUtil.showToast(errorMsg);
    });
  }



}
